package com.ls.lserp.modules.job.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.utils.DateUtils;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.modules.order.dao.QualityResultDao;
import com.ls.lserp.modules.order.entity.QualityResult;
import com.ls.lserp.modules.sys.entity.SendEmailMessage;
import com.ls.lserp.modules.sys.utils.UserUtils;


/** 
 * @ClassName	: QualityResultSyncService 
 * @Description	: 取得当天质检结果数据并发送邮件
 * @author		: shaoyun.tan
 * @date		: 2017年11月20日 
 */
@Service
@Transactional(readOnly = false)
public class QualityResultSyncService {
	
	
	@Autowired
	private QualityResultDao qualityResultDao;//质检结果
	
	/**
	 * 获取相应时间和质检工序数量
	 * @param operatingId
	 * @param dateTime
	 * @return
	 */
	public int getQualityNumToday(int operatingId,String dateTime){
		return qualityResultDao.getQualityNumToday(operatingId,dateTime);
	}
	
	/**
	 * 获取相应时间和质检工序未通过数量
	 * @param operatingId
	 * @param dateTime
	 * @return
	 */
	public int getQualityNotAccNumToday(int operatingId,String dateTime){
		return qualityResultDao.getQualityNotAccNumToday(operatingId,dateTime);
	}
	
	/**
	 * 获取相应时间和质检工序 未通过的各项目数量
	 * @param operatingId
	 * @param dateTime
	 * @return
	 */
	public List<QualityResult> findNotAccessItemNum(int operatingId,String dateTime){
		return qualityResultDao.findNotAccessItemNum(operatingId,dateTime);
	}
	
	/** 
	 * @Title       : sendEmailAboutQualityResult 
	 * @Description : 取得当天质检结果数据并发送邮件
	 * @throws  	: Exception
	 * @author      : shaoyun.tan
	 * @date        : 2017年11月20日
	 */
	public void sendEmailAboutQualityResult() throws Exception {
		//获取当天日期
		String thisTime = DateUtils.getDate("yyyy-MM-dd");
		//获取当天 整烫前（34）质检工序数量
		int quality34Num = getQualityNumToday(34,thisTime);
		//获取当天 整烫前（34）质检工序未通过数量
		int quality34NotAccNum = getQualityNotAccNumToday(34,thisTime);
		//获取当天 整烫前（34）未通过的各项目数量
		List<QualityResult> qResultList34 = findNotAccessItemNum(34,thisTime);
		//获取当天 整烫前（35）质检工序数量
		int quality35Num = getQualityNumToday(35,thisTime);
		//获取当天 整烫前（35）质检工序未通过数量
		int quality35NotAccNum = getQualityNotAccNumToday(35,thisTime);
		//获取当天 整烫前（35）未通过的各项目数量
		List<QualityResult> qResultList35 = findNotAccessItemNum(35,thisTime);
		//获取当天 整烫前（37）质检工序数量
		int quality37Num = getQualityNumToday(37,thisTime);
		//获取当天 整烫前（37）质检工序未通过数量
		int quality37NotAccNum = getQualityNotAccNumToday(37,thisTime);
		//获取当天 整烫前（37）未通过的各项目数量
		List<QualityResult> qResultList37 = findNotAccessItemNum(37,thisTime);
		//保存成功给指定人员发送邮件
		String url =  ResourcesUtil.getConfig("address")+"/quality/result/";
		//整烫前（34）质检
		String result34 = "";
		//整烫前（34）质检未通过明细
		String resultDetail34 = "";
		//整烫前（35）质检
		String result35 = "";
		//整烫前（35）质检未通过明细
		String resultDetail35 = "";
		//整烫前（37）质检
		String result37 = "";
		//整烫前（37）质检未通过明细
		String resultDetail37 = "";
		result34 = ResourcesUtil.getConfig("erp_send_mail_textMessage09_01_34").replace("X_OPERATING_X", "整烫前质检");
		if (qResultList34 != null && qResultList34.size() > 0) {
			for(QualityResult qResult : qResultList34){
				resultDetail34 = resultDetail34 
						+ ResourcesUtil.getConfig("erp_send_mail_textMessage09_02")
						.replace("X_OPERATING_X", "整烫前")
						.replace("X_ITEM_X", qResult.getQcItem())
						.replace("X_NUM_X", qResult.getNotAccessNum()+"");
			}
			resultDetail34 = resultDetail34 + "<br>";
		}else{
			resultDetail34 = "无。<br><br>";
		}
		result35 = ResourcesUtil.getConfig("erp_send_mail_textMessage09_01_35").replace("X_OPERATING_X", "整烫后质检");
		if (qResultList35 != null && qResultList35.size() > 0) {
			for(QualityResult qResult : qResultList35){
				resultDetail35 = resultDetail35 
						+ ResourcesUtil.getConfig("erp_send_mail_textMessage09_02")
						.replace("X_OPERATING_X", "整烫后")
						.replace("X_ITEM_X", qResult.getQcItem())
						.replace("X_NUM_X", qResult.getNotAccessNum()+"");
			}
			resultDetail35 = resultDetail35 + "<br>";
		}else{
			resultDetail35 = "无。<br><br>";
		}
		result37 = ResourcesUtil.getConfig("erp_send_mail_textMessage09_01_37").replace("X_OPERATING_X", "成品尺寸质检");
		if (qResultList37 != null && qResultList37.size() > 0) {
			for(QualityResult qResult : qResultList37){
				resultDetail37 = resultDetail37 
						+ ResourcesUtil.getConfig("erp_send_mail_textMessage09_02")
						.replace("X_OPERATING_X", "成品尺寸")
						.replace("X_ITEM_X", qResult.getQcItem())
						.replace("X_NUM_X", qResult.getNotAccessNum()+"");
			}
			resultDetail37 = resultDetail37 + "<br>";
		}else{
			resultDetail37 = "无。<br><br>";
		}
        SendEmailMessage sendEmailMessage = new SendEmailMessage();
        sendEmailMessage.setDataTheme(ResourcesUtil.getConfig("erp_send_mail_theme09"));
        sendEmailMessage.setDataMessage(result34+resultDetail34+result35+resultDetail35+result37+resultDetail37);
        sendEmailMessage.setDataXXX03(quality34Num + "");
        sendEmailMessage.setDataXXX04(quality34NotAccNum + "");
        sendEmailMessage.setDataXXX05(quality35Num + "");
        sendEmailMessage.setDataXXX06(quality35NotAccNum + "");
        sendEmailMessage.setDataXXX07(quality37Num + "");
        sendEmailMessage.setDataXXX08(quality37NotAccNum + "");
        sendEmailMessage.setDataXURLX(url);
        sendEmailMessage.setDataYYMMDD01(thisTime);
        sendEmailMessage.setMailFlag("qualityControl");
        //发送邮件
        UserUtils.sendEmail(sendEmailMessage);
	}
	
}
